<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/11/7
  Time: 21:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/mb/jsp/common.jsp" %>

<%@ page import="com.wsu.search.tools.Tools" %>

<%
    //取得url带过来的参数
    //String orderidlist = Tools.RmNull(request.getParameter("orderidlist"));
    String projectid = Tools.RmNull(request.getParameter("projectid"));


%>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的收货地址</title>

    <script src="mb/js/jquery.cityselect.js"></script>

</head>
<body ontouchstart="">
<div class="detailTop pf w100 of">
    <div class="w100 of tc c3 f18">
        <span class="pa" style="left: 5px;" onclick="javascript:history.back();"><img src="mb/images/top_back.png"
                                                                                      style="margin-top: 4px;height: 40px;width: 40px;"/></span>
        我的收货地址
    </div>
</div>

<div class="container jz h100" style="padding-top: 41px;">

    <section class="w88 jz of" style="margin-top: 35px;">
        <div id="city_3" class="w100 of clearfix">
            <div class="fl f14" style="width: 50%;line-height: 38px;">
                省份：<select class="prov" id="prov" required="required"></select>
            </div>
            <div class="fr f14" style="width: 50%;line-height: 38px;">
                城市：<select class="city" id="city" disabled="disabled" required="required"></select>
            </div>
        </div>
    </section>
    <section class="w88 jz of" style="margin-top: 15px;">
        <div class="addressBlock w100">
            <textarea required="required" placeholder="详细地址" id="detailAddress"></textarea>
        </div>
    </section>
    <section class="w88 jz of" style="margin-top: 15px;">
        <div class="addressBlock w100">
            <input type="text" name="username" value="" placeholder="联系人" required="required" id="username"
                   class="addressInput"/>
        </div>
    </section>
    <section class="w88 jz of" style="margin-top: 15px;">
        <div class="addressBlock w100">
            <input type="tel" name="usertel" value="" placeholder="联系电话" required="required" id="usertel"
                   class="addressInput"/>
        </div>
    </section>
    <div class="detailBottm pf w100 of">
        <button type="button" class="db w100 f18 cf f18 tc" id="addressSub">确定收货地址</button>
    </div>

</div><!--container-->
<script>

    var projectid = <%=projectid%>;


    function proEstimate() {
        var provVal = $.trim($(".prov").find("option:selected").val());
        var cityVal = $.trim($(".city").find("option:selected").val());
        if (!(provVal == "请选择") && !(cityVal == "请选择")) {
            var info = {"prov": provVal, "city": cityVal};
            return info;
        } else {
            return false;
        }
    }
    
    
    //详细地址过滤下划线等符号
	function Test_email(detailAddress) { 
		var myReg = /^[^_\x22]+$/;
		if (myReg.test(detailAddress)){ return true;}else{ return false; }
	}
    

    $(function () {
        $("#city_3").citySelect({prov: "省份", city: "城市", required: false});

        $('#addressSub').click(function () {
            var info = proEstimate();
            
            
            var prov = info["prov"];
            var city = info["city"];
            var username = $.trim($('#username').val());
            var usertel = $.trim($('#usertel').val());
            var detailAddress = $.trim($('#detailAddress').val());
            
			if (prov == ""||prov == undefined||city == ""||city == undefined) {
                showToast("你尚未选择'省份'或者'城市'")
                return false;
            }
            if (usertel == "") {
                showToast("你还未填写联系电话");
                return false;
            }
            if (username == "") {
                showToast("你还未填写联系人");
                return false;
            }
            if (detailAddress == "") {
                showToast("你还未填写详细地址");
                return false;
            }
            if (Test_email(detailAddress)==false) {
                showToast("详细地址出现未知字符例如下划线等");
                return false;
            }
            // 组装地址 收货地址_联系人_电话（收货地址＝省+市+详细地址）
            var address = prov.split("_")[0] + city.split("_")[0] + detailAddress.split("_")[0] + "_" + username + "_" + usertel;

            //sendOrder(prov.split("_")[1], city.split("_")[1], address);
            window.location.href = "mb/jsp/zhongchouxiadan.jsp?projectid=" + projectid + "&address=" + encodeURIComponent(address) + "&shengid=" + prov.split("_")[1] + "&cityid=" + city.split("_")[1];
        });
    });

    // 申请发货
    function sendOrder(prov, city, address) {
        console.log(JSON.stringify({
            m: 1724,
            orderidlist: orderidlistVal,
            address: address,
            shengid: prov,
            cityid: city
        }));
        $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 1724, orderidlist: orderidlistVal, address: address, shengid: prov, cityid: city}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
                showProgressDg();
            },
            success: function (data) {
                if (data.state == 1) {
                    // 申请成功 关闭页面 回退
                    javascript:history.back();
                } else {
                    // 其他错误弹出提示
                    showToast(data.info);
                }
            },
            error: function (data) {
                showToast(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });
    }

    $().ready(function () {
        //取上次的收货地址
        $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 146}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
                showProgressDg();
            },
            success: function (data) {
                console.log(data);
                if (data.state == 1) {
                    // 申请成功 关闭页面 回退
                    if (typeof(data.obj.address) != "undefined") {
                        // 设置默认地址
                        $.getJSON("mb/js/city.min.js", function (json) {
                            $.each(json.citylist, function (i, prov) {
                                if (prov.p.indexOf(data.obj.shengid) != -1) {
                                    $.each(prov.c, function (i, city) {
                                        if (city.n.indexOf(data.obj.cityid) != -1) {
                                            $("#city_3").citySelect({prov: prov.p, city: city.n, required: false});

                                            // 填写详细地址
                                            var sc = prov.p.split('_')[0] + city.n.split('_')[0];
                                            $("#detailAddress").val(data.obj.address.substring(sc.length, data.obj.address.length));
                                        	$("#username").val(data.obj.person_name);
                                        	$("#usertel").val(data.obj.tel);
                                        	
                                        	return true;
                                        }
                                    });
                                    return true;
                                }
                            });
                        });
                    }

                } else {
                    // 其他错误弹出提示
                    showToast(data.info);
                }
            },
            error: function (data) {
                showToast(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });

    });


</script>
</body>
</html>
